/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.aruoc.sim.siguelineas;

import org.aruoc.sim.SimWorldPainter;
import org.aruoc.sim.SimWorld;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.phys2d.math.Vector2f;
import net.phys2d.raw.strategies.QuadSpaceStrategy;
import org.aruoc.sim.siguelineas.impl.MiRastreador;

/**
 *
 * @mperezma
 */
public class Main {

    public static boolean executing = true;
    public boolean limitarFPS;
    public static final String LINE1 = "Simulador para robot siguelíneas";
    public static final String LINE2 = "ARUOC - Enero 2008";
    public static String LINE3 = "http://www.aruoc.org";
    public static float frameAverage = 0;
    public static long renderTime = 0;
    public static long logicTime = 0;
    public static long inicioSimulacion;

    private void exec() {
        int ii = 0;
        try {


            Vector2f gravedad = new Vector2f(0.0f, 0.0f);

            URL url = this.getClass().getResource("imagen.gif");

            SimWorld world = new SimWorld(gravedad, 10, new QuadSpaceStrategy(20, 5), url);

            ConstructorDialog c = new ConstructorDialog(null, true);
            c.setVisible(true);

            SimWorldPainter painter = new SimWorldPainter(world);
            painter.pintarTerreno = c.isPintarTerreno();
            painter.dibujarEstela = c.isDibujarEstela();

            if (c.isParametrizado()) {
                MiRastreador sim = new MiRastreador();
                sim.setWorld(world);
                sim.setDisntanciaX(c.getDistanciaX());
                sim.setDisntanciaY(c.getDistanciaY());
                sim.setRozamiento(c.getRozamiento());
                sim.potencia = c.getPotencia();
                sim.construct();
                sim.autonomo = c.isAutonomo();

            } else {
                Class clase = ClassLoader.getSystemClassLoader().loadClass(c.getClassName());
                Object instancia = clase.newInstance();

                ((Siguelineas) instancia).setWorld(world);
                ((Siguelineas) instancia).construct();
            }




            float retardo = c.getRetardo();//1000 / 100.0f;
            frameAverage = retardo;
            long lastFrame = System.currentTimeMillis();
            float yield = 10000f;
            float damping = 0.1f;


            System.out.println("Iniciando simulación...");
            inicioSimulacion = System.currentTimeMillis();

            while (executing) {

                long timeNow = System.currentTimeMillis();
                frameAverage = (frameAverage * 10 + (timeNow - lastFrame)) / 11;
                lastFrame = timeNow;

                yield += yield * ((retardo / frameAverage) - 1) * damping + 0.05f;

                for (int i = 0; i < yield; i++) {
                    Thread.yield();
                }

                long beforeRender = System.currentTimeMillis();

                if (c.isSaltarEscenas()) {
                    if (++ii > c.getSaltarEscenas()) {
                        ii = 0;
                        painter.draw();
                    }
                }
                else
                    painter.draw();

                renderTime = System.currentTimeMillis() - beforeRender;

                long beforeLogic = System.currentTimeMillis();

                world.step();

                logicTime = System.currentTimeMillis() - beforeLogic;

            }

            System.out.println("Fin simulación.");
            System.out.println("Duración = " + (System.currentTimeMillis() - inicioSimulacion)+"ms");
            System.out.println("Número total de escenas = " + world.currentTime());
            System.exit(0);

        } catch (InstantiationException ex) {
            System.out.println("No puede instanciarse la clase especificada");
        } catch (IllegalAccessException ex) {
            System.out.println("No puede accederse a la clase especificada");
        } catch (ClassNotFoundException ex) {
            System.out.println("No se encuentra la clase especificada");
        }

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
//        System.out.println(LINE1);
//        System.out.println(LINE2);
//        System.out.println(LINE3);
        Main main = new Main();
        main.exec();

    }
}
